<?php
/*
	参考网址:http://blog.csdn.net/tsunlight/article/details/7409166
*/
class pexcel {
		
	public $file_dir;
	public $name;
	public $English = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public $objPHPExcel;
	public $isShowTitle=0;//显示头部
	public $rows=1;//行数
	public $columns=1;//列数
	public $stop=0;//停止执行
	public $Debug;
		
	public function __construct(){
		Vendor("PHPExcel");
		Vendor("PHPExcel.IOFactory");
		$this->objPHPExcel = new PHPExcel();
		$this->commonTitle();
	}
	
	/* 
	*	commonTitle 
	*	公共头部
	*/
	private function commonTitle(){
		$this->objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
		$this->objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
		$this->objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
		$this->objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
		$this->objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
		$this->objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");
		$this->objPHPExcel->getProperties()->setCategory("Test result file");	
	}

	/* 
	*	commonFoot 
	*	公共底部
	*/
	public function commonFoot($footMsg){
		$row = $this->row + 1;
		$columns = $this->columns - 2;
		$rt =  mb_substr($this->English, $columns, 1);
		$this->objPHPExcel->getActiveSheet()->mergeCells('A'.$row.':'.$rt.$this->columns);
		$footMsg = empty($footMsg) ? "生成时间:".date("Y-m-d H:i:s") : $footMsg;
		$this->objPHPExcel->getActiveSheet()->setCellValue('A'.$row,$footMsg);
	}

	/* 
	*	setContent 
	*	设置头部
	*/
	public function setTitle($top=array()){
		if(!empty($top)){
			$this->isShowTitle=1;
			foreach($top as $k=>$w){
				$rt =  mb_substr($this->English, $k, 1);
				$this->objPHPExcel->getActiveSheet()->setCellValue($rt.'1',$w);
			}
		}
	}
	
	/* 
	*	setContent 
	*	设置内容
	*/
	public function setContent($list){
		if(empty($list)){
			echo "list -> empty";
			return;
		}
		$this->row = $this->isShowTitle == 1 ? count($list) + 1 : count($list);//行数
		$this->showBug();
		foreach($list as $k=>$v){		
			if($this->isShowTitle==1){//是否开启头部
				$k = $k + 1;
			}
			$j=0;
			foreach($v as $kk=>$w){
				$rt =  mb_substr($this->English, $j, 1);
				$this->objPHPExcel->getActiveSheet()->setCellValue($rt.($k+1),$w);
				$j++;
			}
		}
		$this->columns = $this->isShowTitle == 1 ? $j + 1 : $j;	
	}

	
	/* 
	*	save 
	*	保存
	*/
	public function save(){
		if($this->stop)return;
		if(empty($this->file_dir) || empty($this->name)){
			echo "file_dir,name -> empty";
			return;
		}
		$this->objPHPExcel->getActiveSheet()->setTitle('Formulas');		
		$this->objPHPExcel->setActiveSheetIndex(0);
		$objWriter = PHPExcel_IOFactory::createWriter($this->objPHPExcel, 'Excel2007');
		$objWriter->save($this->file_dir.$this->name);
	}

	/* 
	*	download 
	*	下载
	*/	
	public function download(){
		if($this->stop)return;
		if(!file_exists($this->file_dir.$this->name)){//找不到文件
			alert('找不到文件');
		}else{
			$file = fopen($this->file_dir.$this->name,"r");
			Header("Content-type: application/octet-stream");
			Header("Accept-Ranges: bytes");
			Header("Accept-Length: ".filesize($this->file_dir . $this->name));
			Header("Content-Disposition: attachment; filename=".$this->name);
			echo fread($file, filesize($this->file_dir.$this->name));
			fclose($file);
		}	
	}

	/* 
	*	alert 
	*	显示
	*/	
	private function alert($msg){
		echo '<script type="text/javascript">alert("'.$msg.'");history.go(-1);</script>';
	}
	
	public function showBug(){
		echo $this->Debug;
	}
	
}
?>